package edu.aysf.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


// nacos的配置修改后，动态刷新到本地
@RefreshScope
@RequestMapping("/order")
@RestController
public class HelloController {
    // hello为msg的默认值
//    @Value("${msg:hello}")
    @Value("${msg}")
    private String message;

    @RequestMapping("/hello")
    public String getMessage() {
        return message;
    }

    @RequestMapping("/head")
    public void getHead(@RequestHeader String requestHeader,
                        @RequestParam  String requestParam
                        ) throws IOException, URISyntaxException, InterruptedException {
        System.out.println("请求头：" + requestHeader);
        System.out.println("请求参数：" + requestParam);

        // 创建HttpClient实例
        HttpClient client = HttpClient.newHttpClient();

        // 创建请求
        HttpRequest request = HttpRequest.newBuilder()
                // 替换为你想访问的URL
                .uri(new URI("http://localhost:9000/api/order/head?requestParam=ding"))
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        // 获取响应头
        HttpHeaders headers = response.headers();

        // 获取响应头中指定的响应头
        String[] responseHeader = headers.allValues("responseHead").toArray(new String[0]);
        // 打印所有响应头
        for (String header : responseHeader) {
            System.out.println("响应头:" + header);
        }

        // 获取响应头中指定的cookie
        String[] cookies = headers.allValues("name").toArray(new String[0]);

        // 打印所有cookie
        for (String cookie : cookies) {
            System.out.println("cookie:" + cookie);
        }
    }

}
